/**
 * VariableSettingServiceImpl.java
 * @author
 * 作者 屈甲康 
 */
package org.jackysoft.service.impl;

import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.VariableSetting;
import org.jackysoft.service.AbstractIdentifiedService;
import org.jackysoft.service.VariableSettingService;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
@Service
@Transactional
public class VariableSettingServiceImpl 
extends AbstractIdentifiedService<VariableSetting>
implements VariableSettingService {
    /**
	 * 
	 */
	private static final long serialVersionUID = 6203565154295205617L;
	final static Log logger = LogFactory.getLog(VariableSettingServiceImpl.class);

	public VariableSettingServiceImpl(){
	    this.type = VariableSetting.class;
	}

	@Override
	public VariableSetting get(String workflowname, String name) {
		String hql = String.format("select new VariableSetting(v.workflowName,v.label,v.name,v.value)  from %s v where v.workflowName='%s' and v.name='%s'", entitytable(),workflowname,name);
		Iterator itr = getSession().createQuery(hql).iterate();
		return (VariableSetting) (itr.hasNext()?itr.next():null);
	}

	@Override
	public void remove(String workflowName,String name) {
		String hql = String.format("delete from %s where workflowName='%s' and name='%s'", entitytable(),workflowName,name);
		getSession().createQuery(hql).executeUpdate();
		
	}

	@Override
	public Collection<VariableSetting> getsByWorkflow(String workflow) {
		String hql = String.format("from %s v where v.workflowName.workflowName='%s'",entitytable(),workflow);
		return getSession().createQuery(hql).list();
	}

	@Override
	public void removeByWorkflow(String workflow) {
		String hql = String.format("delete from %s where workflowName='%s' ", entitytable(),workflow);
		getSession().createQuery(hql).executeUpdate();
		
	}

	@Override
	public void create(Collection<VariableSetting> bs)
			throws DataAccessException {
		if(CollectionUtils.isEmpty(bs))return ;
		VariableSetting[] bss= bs.toArray(new VariableSetting[0]);
		String workflowName = bss[0].getWorkflowName();
		removeByWorkflow(workflowName);
		super.create(bs);
	}
	
	
		
}